﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using TuyenSinh.DLL;
using TuyenSinh.Ultilities;

namespace TuyenSinh.DAL
{
    public class DAMajor : BaseDAL
    {
        public DataSet GetField()
        {
            DataSet ds = new DataSet();
            try
            {
                DbCommand cmd;
                cmd = DB.GetStoredProcCommand("sp_GetField");

                ds = DB.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                Logger.FrameworkLogger.Error("Get Field ", ex);
                return null;
            }
            return ds;
        }

        public DataSet GetSesion()
        {
            DataSet ds = new DataSet();
            try
            {
                DbCommand cmd;
                cmd = DB.GetStoredProcCommand("sp_GetSession");

                ds = DB.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                Logger.FrameworkLogger.Error("Get Session ", ex);
                return null;
            }
            return ds;
        }

        public bool InsertMajor(string name, string desc,  int sessionId, int schoolId, int iGroupMajorId, int isActive, int isDelete)
        {
            bool bResult = false;
            DbCommand cmd;

            cmd = DB.GetStoredProcCommand("sp_InsertMajor");
            DB.AddInParameter(cmd, "@Name", SqlDbType.NVarChar, name);
            DB.AddInParameter(cmd, "@SessionId", SqlDbType.Int, sessionId);
            DB.AddInParameter(cmd, "@Desc", SqlDbType.NVarChar, desc);
            DB.AddInParameter(cmd, "@SchoolId", SqlDbType.Int, schoolId);
            DB.AddInParameter(cmd, "@GroupMajor", SqlDbType.Int, iGroupMajorId);

            DB.AddInParameter(cmd, "@IsActive", SqlDbType.Bit, isActive == 1);
            DB.AddInParameter(cmd, "@IsDelete", SqlDbType.Bit, isDelete == 1);

            try
            {
                DB.ExecuteNonQuery(cmd);
                bResult = true;
            }
            catch (Exception ex)
            {
                Logger.FrameworkLogger.Error("System Error - Insert Major", ex);
                bResult = false;
            }
            return bResult;
        }

        public bool InsertMajorDetail(int iSchoolId, int iMajorId, int iNumRegister, int iTarget, float iScore, int iYear, float fNumCompetitor)
        {
            bool bResult = false;
            DbCommand cmd;

            cmd = DB.GetStoredProcCommand("sp_InsertMajorDetail");
            DB.AddInParameter(cmd, "@SchoolId", SqlDbType.Int, iSchoolId);
            DB.AddInParameter(cmd, "@MajorId", SqlDbType.Int, iMajorId);
            DB.AddInParameter(cmd, "@NumRegister", SqlDbType.Int, iNumRegister);
            DB.AddInParameter(cmd, "@Target", SqlDbType.Int, iTarget);
            DB.AddInParameter(cmd, "@Score", SqlDbType.Float, iScore);
            DB.AddInParameter(cmd, "@Year", SqlDbType.Char, iYear);
            DB.AddInParameter(cmd, "@Competitor", SqlDbType.Float, fNumCompetitor);

            try
            {
                DB.ExecuteNonQuery(cmd);
                bResult = true;
            }
            catch (Exception ex)
            {
                Logger.FrameworkLogger.Error("System Error - Insert Major Detail", ex);
                bResult = false;
            }
            return bResult;
        }

        public DataSet GetLastMajor()
        {
            DataSet ds = new DataSet();
            DbCommand cmd;
            cmd = DB.GetStoredProcCommand("sp_GetLastIdMajor");

            try
            {
                ds = DB.ExecuteDataSet(cmd);
            }
            catch (Exception exception)
            {
                Logger.FrameworkLogger.Error("System Error", exception);
                ds = null;
            }
            return ds;
        }

        public DataSet GetGroupMajor(int id, string sName)
        {
            DataSet ds = new DataSet();
            DbCommand cmd;
            cmd = DB.GetStoredProcCommand("sp_GetGroupMajor");
            if (id!=0)
            {
                DB.AddInParameter(cmd, "@Id", SqlDbType.Int, id);                
            }
            if (!string.IsNullOrEmpty(sName))
            {
                DB.AddInParameter(cmd, "@Name", SqlDbType.Int, sName);                
            }
            try
            {
                ds = DB.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                ds = null;
                Logger.FrameworkLogger.Error("System Error", ex);               
            }
            return ds;
        }

    }
}
